
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>12.3.&nbsp;步入 SOAP</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;12&nbsp;章&nbsp;SOAP Web 服务">
      <link rel="previous" href="install.html" title="12.2.&nbsp;安装 SOAP 库">
      <link rel="next" href="debugging.html" title="12.4.&nbsp; SOAP 网络服务查错">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">SOAP Web 服务</a>&nbsp;&gt;&nbsp;<span class="thispage">步入 SOAP</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="install.html" title="上一页: “安装 SOAP 库”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="debugging.html" title="下一页: “ SOAP 网络服务查错”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="soap.firststeps"></a>12.3.&nbsp;步入 <span class="acronym">SOAP</span></h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>调用远程函数是 <span class="acronym">SOAP</span> 的核心功能。有很多提供公开 <span class="acronym">SOAP</span> 访问的服务器提供用于展示的简单功能。
            </p>
         </div>
         <p>最受欢迎的 <span class="acronym">SOAP</span> 公开访问服务器是 <a href="http://www.xmethods.net/">http://www.xmethods.net/</a>。这个例子使用了一个展示函数，可以根据美国邮政编码返回当地气温。
         </p>
         <div class="example"><a name="d0e30468"></a><h3 class="title">例&nbsp;12.6.&nbsp;获得现在的气温</h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>from</span> SOAPpy <span class='pykeyword'>import</span> SOAPProxy</span>            <a name="soap.firststeps.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">url = <span class='pystring'>'http://services.xmethods.net:80/soap/servlet/rpcrouter'</span></span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">namespace = <span class='pystring'>'urn:xmethods-Temperature'</span></span>  <a name="soap.firststeps.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">server = SOAPProxy(url, namespace)</span>      <a name="soap.firststeps.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">server.getTemp(<span class='pystring'>'27502'</span>)</span>                 <a name="soap.firststeps.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">80.0</span>
</pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#soap.firststeps.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">你通过 <tt class="classname">SOAPProxy</tt> 这个代理 (proxy) 类访问远程 <span class="acronym">SOAP</span> 服务器。这个代理处理了所有的 <span class="acronym">SOAP</span> 内部事务，其中包括：根据函数名和参数列表创建 XML 请求文档，并将这个请求文档通过 HTTP 发送到远程 <span class="acronym">SOAP</span> 服务器；解析 XML 返回文档，并创建本地的 <span class="application">Python</span> 返回值。在下一节中你将看到这个 XML 文档。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#soap.firststeps.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">每个 <span class="acronym">SOAP</span> 服务都有一个 <span class="acronym">URL</span> 用以处理所有请求。相同的 <span class="acronym">URL</span> 可以用于所有的函数请求。每个特定服务则只有一个函数。但稍后你将看到的 Google <span class="acronym">API</span> 却有多个函数。这个服务的 <span class="acronym">URL</span> 提供给所有函数分享。每个 <span class="acronym">SOAP</span> 服务都有一个命名空间 (namespace)，这个命名空间是由服务器任意命名的。这不过是为调用 <span class="acronym">SOAP</span> 方法设置的。它使得服务器让多个不相关的服务共享服务 <span class="acronym">URL</span> 和路径请求成为可能。这与 <span class="application">Python</span> 中模块相对于<a href="../xml_processing/packages.html" title="9.2.&nbsp;包">包</a>的关系类似。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#soap.firststeps.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">这里你创建了包含服务 <span class="acronym">URL</span> 和服务命名空间的 <tt class="classname">SOAPProxy</tt>。此时还不会连接到 <span class="acronym">SOAP</span> 服务器；仅仅是建立了一个本地 <span class="application">Python</span> 对象。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#soap.firststeps.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">到此为止，如果你的设置完全正确，应该可以向调用本地函数一样调用远程 <span class="acronym">SOAP</span> 方法。这和给普通函数传递参数并接收返回值一样，但在背后却隐藏着很多的工作。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>让我们看一看这些背后的工作。</p>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="install.html">&lt;&lt;&nbsp;安装 SOAP 库</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#soap.divein" title="12.1.&nbsp;概览">1</a> <span class="divider">|</span> <a href="install.html" title="12.2.&nbsp;安装 SOAP 库">2</a> <span class="divider">|</span> <span class="thispage">3</span> <span class="divider">|</span> <a href="debugging.html" title="12.4.&nbsp; SOAP 网络服务查错">4</a> <span class="divider">|</span> <a href="wsdl.html" title="12.5.&nbsp;WSDL 介绍">5</a> <span class="divider">|</span> <a href="introspection.html" title="12.6.&nbsp;以 WSDL 进行 SOAP 内省">6</a> <span class="divider">|</span> <a href="google.html" title="12.7.&nbsp;搜索 Google">7</a> <span class="divider">|</span> <a href="troubleshooting.html" title="12.8.&nbsp; SOAP 网络服务故障排除">8</a> <span class="divider">|</span> <a href="summary.html" title="12.9.&nbsp;小结">9</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="debugging.html"> SOAP 网络服务查错&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>